﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start;
            return input.slice(start);
        }
        return [];
    }
});

app.controller('ChildDedicationController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.fileChangedBirthCertificate = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.ChildDedication.PhotoCopyOfBirthCertificate = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.fileChangedApplicationCopy = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.ChildDedication.PhotoCopyOfApplication = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.fileChangedChildImage = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.ChildDedication.ImageOfChild = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.Status = GlobalService.GetStatus();

    $scope.selectFamily = function () {
        $("#SelectFamily").modal({ backdrop: 'static' });
        BindFamilyDetails();
    }

    
    $scope.GetFamilyDetails = function (data) {
        console.log(data);
        $scope.ChildDedication = {}
        
        $scope.ChildDedication.FamilyName = data.FamilyName;
        $scope.ChildDedication.FamilyId = data.FamilyId;
        $scope.ChildDedication.CountryId = data.PresentCountry;
        $scope.ChildDedication.StateId = data.PresentState;
        $scope.ChildDedication.PresentCity = data.PresentCity;
        $scope.ChildDedication.PresentAdress = data.PresentAddress;
        $scope.ChildDedication.PresentPin = data.PresentPinCode;
        $scope.ChildDedication.YearOfMemberShip = data.SinceWhen;
    }

    function BindFamilyDetails() {
        url = "Members/GetMembers";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.FamiliesDetails = data;
        });
    }

    GlobalService.GetMembers().success(function (data) {
        $scope.familydetails = data;
    });

    $scope.ChildDedication = {};
    $scope.GetValues = function (data, familydetails, idx) {
        
    }

    $scope.select = function () {
        $("#SelectMember").modal('hide');
    }

    GlobalService.GetCountry().success(function (data) {
        $scope.countries = data;
    });

    $scope.$watch('ChildDedication.CountryId', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            GlobalService.GetStates(newVal).success(function (data) {
                $scope.states = data;
            });
        }
    });

    $scope.Genders = GlobalService.GetGenders();

    $scope.BindBasicDetails = function (data) {
        console.log(data);
    }

    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Child Dedication Details";
                $scope.ChildDedication = null;
                break;
            case 'edit':
                $scope.title = "Edit Child Dedication Details";
                url = "ChildDedication/GetChildDedication";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.ChildDedication = data;
                });
                break;
        }
        $("#ChildDedication").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "ChildDedication/GetChildDedications";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            console.log($scope.list);
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Baptism/DeleteBaptism";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "ChildDedication/AddChildDedication";
                break;
            case 'edit':
                url = "ChildDedication/UpdateChildDedication";
                break;
        }
        ApplicationService.Save($scope.ChildDedication, url).success(function (result) {
            alertboxservice.ShowMsg(result, "ChildDedication ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

}]);

